<?php
namespace Api\Controller;
use Think\Controller;
class UserController extends Controller{
	//展示用户信息
	public function index(){
		echo "sldlksjdlkasldkj";
	}
	
	//短信验证码
	public function sendCode(){
		//获取前台输入的手机号码
		$mobile=$_POST['mobile'];
		$code = $this->code();
		$data="您好！验证码是：$code";
		//2.发送短信
		sendSms($mobile, $data);
	}
	//生成验证码
	public function code(){
		$code = rand(100000,999999);
		$data = $code ;
		$_SESSION['code'] = $code;
		return $data;
	}
	//验证验证码
	public function testCode(){
		$mobile =trim($_POST['mobile']);
		$code = $_POST['code'];
	
		if ( empty( $mobile )  || empty( $code ) )
		{
			$value = ["code" =>"empty"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		if ($code!=$_SESSION['code'])
		{
// 			$value = ["code" =>"false"];
// 			header("Content-Type: text/json");
// 			die(json_encode($value));
		}
		//验证吗验证正确
		//检验用户是否已经注册
		$user=D("User");
		$users=$user->where("mobile=$mobile")->find();
	
		if($users){
			//有数据
			//则电话已经注册过来了，不能重复注册
			$value = ["code" =>"already"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			//没有，新建
			$password=$_POST['password'];
			$user->mobile=$mobile;
			$user->password = md5("$password");
			$user->createtime = time();
			$user->gender = $_POST["gender"];
			$user->username = $_POST["username"];
			$user->email = $_POST["email"];
			$user->create_time=time();
			
			
			if($user->add()){
				$users=$user->where("mobile=$mobile")->find();
				$value = ["code" =>"success","data"=>$users];
				header("Content-Type: text/json");
				die(json_encode($value));
			}else{
				$value = ["code" =>"wrong","data"=>""];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
			
		}

	}
	//登录
	public function login(){
		$mobile = $_POST["mobile"];
		$password = $_POST["password"];
		if (empty($mobile) || empty($password)){
			$value = ["code" =>"empty"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		$password = md5($password);
		$data = M("User")->where("mobile = $mobile  and password = '$password'")->find();
		if ($data){
			
			$u=M("User");
			$u->id = $data["id"];
			$u->is_login = 1;
			if($u->save()){$data["is_locate"]=1;}
			$value = ["code" =>"success","user"=>$data];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	//更新用户信息
	public function updateUser(){
	
		$user = M("User");
		$id=$_POST['user_id'];
		$user->id = $id;
	
		if($_POST['username']!=null){
				
			$user->username = $_POST["username"];
		}
		if($_POST['email']!=null){
		
			$user->email = $_POST["email"];
		}
		if($_POST['address']!=null){
		
			$user->address = $_POST["address"];
		}
		
		if(isset($_FILES["avatar"])){
			if($_FILES['avatar']['error'] == 0){
					
				$upload= new \Think\Upload();
				$upload->autoSub=true; //开启子目录保存文件
				$upload->maxSize = 2097152;//最大为2m
				$upload->subName=array('date','Y-m-d');//以时间格式生成文件夹
				$upload->exts=array('jpg','jpeg','gif','png','bmp');//可上传的类型
				$upload->savePath='User/';
				$info=$upload->upload();
				//dump($info);
					
				if(!$info){
					$this->error($upload->getError(),'add',3);
				}
					
				$user->avatar=$info['avatar']['savepath'].$info['avatar']['savename'];
			}
		}
		if($user->save()){
			$users=$user->where("id=$id")->find();
			$value = ["code" =>"success","data"=>$users];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	
	}
	
	//忘记密码
	public function pwChange(){
		$mobile = $_POST["mobile"];
		$code = $_POST['code'];
		$password = $_POST["password"];
		
		if ( empty( $mobile )  || empty( $code ) )
		{
			$value = ["code" =>"empty"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		$user = M("User");
		$data = $user->where("mobile = $mobile")->find();
		if(!$data){
			$value = ["code" =>"noregister"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		if ($code!=$_SESSION['code'])
		{
// 			$value = ["code" =>"false"];
// 			header("Content-Type: text/json");
// 			die(json_encode($value));
		}
		
		$user->password = md5($password);
		if ($user->where("mobile = $mobile")->save()){
			$value = ["code" =>"success"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		
	}
	//填写安装工信息
	public function erectorChange(){
		$user_id = $_POST["user_id"];
		$account_type = $_POST["account_type"];
		$account_num = $_POST["account_num"];
		$realname = $_POST["realname"];
		$identity_card = $_POST["identity_card"];
		$user = M("User");
		$erector = M("Erector");
		$is_apply = $erector->where("user_id = $user_id")->find()?1:0;
		$erector->user_id = $user_id;
		$erector->account_type = $account_type;
		$erector->account_num = $account_num;
		$erector->realname = $realname;
		$erector->identity_card = $identity_card;
		$erector->status = 0;
		$erector->apply_time = time();
	if(isset($_FILES["idcard_image"])){
			if($_FILES['idcard_image']['error'] == 0){
					
				$upload= new \Think\Upload();
				$upload->autoSub=true; //开启子目录保存文件
				$upload->maxSize = 4194304;//最大为2m  2097152 
				$upload->subName=array('date','Y-m-d');//以时间格式生成文件夹
				$upload->exts=array('jpg','jpeg','gif','png','bmp');//可上传的类型
				$upload->savePath='ErectotID/';
				$info=$upload->upload();
				//dump($info);
					
				if(!$info){
					$value = ["code" =>"wrong","massage"=>$upload->getError()];
					header("Content-Type: text/json");
					die(json_encode($value));
				}
					
				$erector->idcard_image = $info['idcard_image']['savepath'].$info['idcard_image']['savename'];
			}
		}
		if ($is_apply){
			if($erector->where("user_id = $user_id")->save()){
				$value = ["code" =>"success"];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			if($erector->add()){
				$value = ["code" =>"success"];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		
		
	}
	public function checkErector(){
		$user_id = $_REQUEST["user_id"];
// 		dump($user_id);
		$is_erector = M("User")->where("id = $user_id")->find()["is_erector"];
		$apply_status = M("Erector")->where("user_id = $user_id")->find();
		$apply_status = $apply_status?$apply_status["status"]:-2;
		$value = ["is_erector" =>$is_erector,"apply_status"=>$apply_status];
		header("Content-Type: text/json");
		die(json_encode($value));
	}
	public function logout(){
		$user = M("User");
		$user->id = $_POST["user_id"];
		$user->is_login = 0;
		if($user->save()){
			$value = ["code" =>"success"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	public function addBandcard(){
		$erector_id = $_POST["erector_id"];
		$account_type=$_POST["account_type"];
		$account_num = $_POST["account_num"];
		$account_name = $_POST["account_name"];
		$account_idcard = $_POST["account_idcard"];
		$account = $_POST["account"];//第几张
		$erector = M("Erector");
		if($account == 1){
			$erector->account_num = $account_num;
			$erector->account_type = $account_type;
			$erector->account_idcard = $account_idcard;
			$erector->$account_name = $$account_name;
		}elseif($account == 2){
			$erector->account2_num = $account_num;
			$erector->account2_type = $account_type;
			$erector->account2_idcard = $account_idcard;
			$erector->account2_name = $account_name;
		}
		if($erector->where("user_id = $erector_id")->save()){
			$ere = $erector->where("user_id = $erector_id")->find();
			$value = ["code" =>"success","data"=>$ere];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	//获取按照工信息
	public function getErector(){
		$id = $_POST["erector_id"];
		$erector=M("Erector")->where("user_id = $id")->find();
		if (M("Erector")->getDbError()){
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		$value = ["code" =>"success","data"=>$erector];
		header("Content-Type: text/json");
		die(json_encode($value));
	}
	
	public function deleteCard(){
		$erector_id = $_POST["erector_id"];
		$account = $_POST["account"];//第几张
		$erector = M("Erector");
		$er = $erector->where("id = $erector_id")->find();
		if ($account == 1){
			$erector->account_type = $er["account_type2"];
			$erector->account_num = $er["account_num2"];
			$erector->account_name = $er["account_name2"];
			$erector->account_idcard = $er["account_idcard2"];
			$erector->account_type2 = "";
			$erector->account_num2 = "";
			$erector->account_name2 = "";
			$erector->account_idcard2 = "";
		}elseif($account == 2){
			$erector->account2_type = "";
			$erector->account2_num = "";
			$erector->account2_name = "";
			$erector->account2_idcard = "";
		}
		if($erector->where("user_id = $erector_id")->save()){
			$ere = $erector->where("user_id = $erector_id")->find();
			$value = ["code" =>"success","data"=>$ere];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	
}